<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        const arr = [{ a: { b: 1 } }, { c: 5 }]
        // weakMap 解决循环引用问题
        function deepclone(target, weakMap = new WeakMap()) {
            const newTarget = Array.isArray(target) ? [] : {}
            for (item in target) {
                if (typeof target[item] === "object") {
                    if (weakMap.has(target[item])) {
                        newTarget[item] = weakMap.get(target[item])
                    } else {
                        weakMap.set(target[item], target[item])
                        newTarget[item] = deepclone(target[item], weakMap)
                    }
                } else {
                    newTarget[item] = target[item]
                }
            }
            return newTarget
        }
        const newArr = deepclone(arr)
        newArr.push({ d: 6 })
        console.log(arr)
        console.log(newArr)
    </script>
    </script>
</body>

</html>